package com.jdy.haoduoaiteacher.model;

import android.os.Parcel;
import android.os.Parcelable;

import java.util.Hashtable;



/**
 * Container of different kinds of message ,which includes its message
 * body,type, and also the state of the message and so on.
 * 
 * @author zp @ 2015-5-18 下午3:12:51
 */
public class LeMessage implements Parcelable, Cloneable {

  private static final String TAG = "msg";

  public LeContact from;

  public LeContact to;

  public MessageBody body;

  public String msgId;

  public boolean isAcked;

  public boolean isDelivered;

  public long msgTime;// Seconds sine 1970-01-01

  public ChatType chatType;

  public Hashtable<String, String> attributes;

  transient boolean unread;

  transient boolean offline;

  boolean isListened;

  static final String ATTR_ENCRYPTED = "isencrypted";

  public LeMessageDirection direction;

  public LeMessageStatus status;

  public String progress;

  private final LeMessageType messageType;

  public String mFilePathName;

  public int mDuration;

  public String headImgUrl;

  public LeMessage(LeMessageType msgType, long ctime) {
    this.attributes = new Hashtable<String, String>();
    this.messageType = msgType;
    this.from = LeSessionManager.getInstance().getLoginUser();
    this.status = LeMessageStatus.SUCCESS;
    this.msgTime = ctime;
  }

  public LeMessage(LeMessageType type, String fromUserName, String toUserName) {
    this(type, System.currentTimeMillis() / 1000);
    this.from = new LeContact(fromUserName);
    this.to = new LeContact(toUserName);
  }

  public MessageBody getBody() {
    // TODO Auto-generated method stub
    return body;
  }

  public ChatType getChatType() {
    // TODO Auto-generated method stub
    return chatType;
  }

  public String getMsgId() {
    // TODO Auto-generated method stub
    return msgId;
  }

  public boolean isListened() {
    // TODO Auto-generated method stub
    return false;
  }

  public LeContact getFrom() {
    // TODO Auto-generated method stub
    return this.from;
  }

  public LeMessageType getType() {
    // TODO Auto-generated method stub
    return messageType;
  }

  public LeContact getTo() {
    // TODO Auto-generated method stub
    return this.to;
  }

  public String getStringAttribute(String key) {
    // TODO Auto-generated method stub
    if (attributes.containsKey(key))
      return attributes.get(key);
    else
      return "1";
  }

  public long getMsgTime() {
    // TODO Auto-generated method stub
    return msgTime * 1000;
  }

  public boolean getBooleanAttribute(String messageAttrIsVoiceCall, boolean b) {
    // TODO Auto-generated method stub
    return false;
  }

  @Override
  public int describeContents() {
    // TODO Auto-generated method stub
    return 0;
  }

  @Override
  public void writeToParcel(Parcel dest, int flags) {
    // TODO Auto-generated method stub

  }

  public static LeMessage createSendMessage(LeMessageType voice,
      LeMessageDirection ld, long ctime, String fileName, int duration,
      String url) {
    // TODO Auto-generated method stub
    LeMessage lm = new LeMessage(voice, ctime);
    lm.mFilePathName = fileName;
    lm.mDuration = duration;
    lm.direction = ld;
    lm.from = LeSessionManager.getInstance().getLoginUser();
    lm.headImgUrl = url;
    return lm;
  }

  // public static LeMessage createSendMessage(LeMessageType voice,
  // LeMessageDirection ld, long ctime, String fileName, int duration) {
  // // TODO Auto-generated method stub
  // LeMessage lm = new LeMessage(voice, ctime);
  // lm.mFilePathName = fileName;
  // lm.mDuration = duration;
  // lm.direction = ld;
  // lm.from = LeSessionManager.getInstance().getLoginUser();
  // return lm;
  // }

  public void setChatType(ChatType group) {
    // TODO Auto-generated method stub
    this.chatType = group;
  }

  public void setReceipt(String toChatUsername) {
    // TODO Auto-generated method stub
    if (LeContactManager.getInstance().contains(toChatUsername)) {
      this.to = LeContactManager.getInstance().getContactByUserName(
          toChatUsername);
    } else {
      LeContact lc = new LeContact(toChatUsername);
      LeContactManager.getInstance().addContactInternal(lc);
      this.to = lc;
    }
  }

  /**
   * Construct from field.
   * 
   * @param username
   */
  public void setFrom(String username) {
    this.from = new LeContact(username);
  }

  public void addBody(VoiceMessageBody body) {
    // TODO Auto-generated method stub
    this.body = body;
  }

  public void setAttribute(String key, String value) {
    // TODO Auto-generated method stub
    if (!attributes.containsKey(key))
      attributes.put(key, value);
  }

  public String getTimeLength() {
    // TODO Auto-generated method stub
    return String.valueOf(mDuration);
  }

}
